<?php 
## include required files
/*******************************/
require_once '../model/users.php';
require_once '../model/moduleuser.php';
## Paginaton class
require_once ("../includes/classes/pagination_class.php");
/*******************************/
## Create Objects
/*******************************/
$userObj = new Model_Users();
$moduleObj=new Model_ModuleUser();
/*******************************/

/*******************************/
##Fetch all admin users from database
/******************************/
if($_GET['q']=='homeowneruserlist')
{
	$moduelArray=$moduleObj->getModuleHelpText(18);
	$smarty->assign('moduelArray', $moduelArray);
}

function order_array_num ($array, $key, $order = "ASC")
    {
        $tmp = array();
        foreach($array as $akey => $array2)
        {
            $tmp[$akey] = $array2[$key];
        }
       
        if($order == "DESC")
        {arsort($tmp , SORT_NUMERIC );}
        else
        {asort($tmp , SORT_NUMERIC );}

        $tmp2 = array();       
        foreach($tmp as $key => $value)
        {
            $tmp2[$key] = $array[$key];
        }       
       
        return $tmp2;
    } 




## Get search parameters in variables - 
if(isset($_POST['search']) != '' && $_POST['search'] != 'Homeowner Name') {
//	echo 'aaaaaa';exit;
	$searchindex = addslashes($_POST['search']);
} else if($_GET['search'] != '') {
	$searchindex = addslashes($_GET['search']);
} else {
	$searchindex = '';
}
// Assign search variable to show in search textbox
$smarty->assign('search',stripslashes (stripslashes ($searchindex)));

if($_GET['col1'] != '') 
{
	$col1 = base64_decode($_GET['col1']);
} 
else if($_GET['col2'] != '') 
{
	$col2 = base64_decode($_GET['col2']);
} 
else 
{
	$col1 = '0';	
}
##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	$rowsPerPage = ROW_PER_PAGE;

	if($_GET['col1'] != '') 
	{
		$pageName = "home.php?q=homeowneruserlist&col1=".$_GET['col1'].""; ;
	} 
	else if($_GET['col2'] != '') 
	{
		$pageName = "home.php?q=homeowneruserlist&col2=".$_GET['col2'].""; ;
	} 
	else 
	{
		$pageName = "home.php?q=homeowneruserlist"; 
	} 

	## Count all the records
	$userArray = $userObj->getAllHomeOwnerUsers_POSTJOBS($searchindex,$col1);
	$total_rows = count($userArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid', $newid);
	$userArray = $userObj->getAllHomeOwnerUsers_POSTJOBS($searchindex,$col1, $rowsPerPage,$offset);
	//echo "<pre>"; print_r($userArray);exit;
	
	for($i=0;$i<count($userArray);$i++)
	{
		$postjobcount = $userObj->getAllHomeOwnerUsersPostJobsCount($userArray[$i]['id']);	
		$userArray[$i]['jobcnt'] = $postjobcount;
	}
	
	if($col2=='0')
	{
		$userArray = order_array_num($userArray,'jobcnt','ASC');
		$userArray = @array_values($userArray);
	}
	if($col2=='1')
	{
		$userArray = order_array_num($userArray,'jobcnt','DESC');
		$userArray = @array_values($userArray);
	}
	$smarty->assign('userArray', $userArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'postjobs');
$smarty->assign('mainmenu', '3');

## Unset all the objects created which are on this page
unset($userObj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'postjobs/homeowneruserlist.tpl');
unset($smarty);
?>